package com.asion.vmovie.activities;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.asion.vmovie.R;
import com.asion.vmovie.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by asion on 16-10-18.
 */

public class WebActivity extends BaseActivity {
    @BindView(R.id.muhou_back)
    ImageView muhouBack;
    @BindView(R.id.muhou_share)
    ImageView muhouShare;
    @BindView(R.id.muhou_webview)
    WebView muhouWebview;
    @BindView(R.id.web_likecount)
    TextView webLikecount;
    @BindView(R.id.web_like)
    LinearLayout webLike;
    @BindView(R.id.web_sharecount)
    TextView webSharecount;
    @BindView(R.id.web_share)
    LinearLayout webShare;
    @BindView(R.id.web_commentcount)
    TextView webCommentcount;
    @BindView(R.id.web_comment)
    LinearLayout webComment;
    @BindView(R.id.web_title)
    TextView webTitle;
    @BindView(R.id.web_bottom)
    LinearLayout webBottom;
    private BottomSheetDialog bottomSheetDialog;


    public static void showAction(Activity activity, Bundle bundle) {
        Intent intent = new Intent(activity, WebActivity.class);
        intent.putExtra("bundle", bundle);
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.fade_in,
                R.anim.fade_out);
    }

    @Override
    public int getLayoutId() {
        return R.layout.web_activity;
    }

    @Override
    public void initPresenter() {
    }

    @Override
    public void initView() {
        startProgressDialog();
        Bundle bundle = getIntent().getBundleExtra("bundle");

        String url = bundle.getString("url");
        String likecount = bundle.getString("likecount");
        String sharecount = bundle.getString("sharecount");
        String commentcount = bundle.getString("commentcount");
        String title = bundle.getString("title");

        if (title != null) {
            webBottom.setVisibility(View.GONE);
            webTitle.setText(title);
        }

        WebSettings settings = muhouWebview.getSettings();
        settings.setDomStorageEnabled(true);
        settings.setJavaScriptEnabled(true);
        muhouWebview.loadUrl(url);
        webCommentcount.setText(commentcount);
        webLikecount.setText(likecount);
        webSharecount.setText(sharecount);

        muhouBack.setOnClickListener(v -> finish());

        muhouWebview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                stopProgressDialog();
            }
        });

        bottomSheetDialog = new BottomSheetDialog(this);
    }

    @OnClick({R.id.muhou_share, R.id.web_share})
    public void onClick(View view) {
        View inflate = getLayoutInflater().inflate(R.layout.share_dialog, null);
        inflate.findViewById(R.id.dialog_close).setOnClickListener(v -> bottomSheetDialog.dismiss());
        inflate.findViewById(R.id.dialog_qq).setOnClickListener(v -> ToastUtil.showShort("qq"));
        inflate.findViewById(R.id.dialog_qzone).setOnClickListener(v -> ToastUtil.showShort("qzone"));
        inflate.findViewById(R.id.dialog_vblog).setOnClickListener(v -> ToastUtil.showShort("vblog"));
        inflate.findViewById(R.id.dialog_wechat).setOnClickListener(v -> ToastUtil.showShort("wechat"));
        bottomSheetDialog.setContentView(inflate);
        bottomSheetDialog.show();
    }

}
